<<<<<<< HEAD
from . import admin_bp 
from flask import request,jsonify
from app.models import ModuleType,OperationType,User
from app.models import OperationLog,Role,Permission
from app.extensions import db
from app.decorator import permission_required

@admin_bp.route('/admin/permission/list',methods=['GET'])
=======
from . import admin_bp
from flask import request, jsonify
from app.models import ModuleType, OperationType, User
from app.models import OperationLog, Permission
from app.extensions import db
from app.decorator import permission_required


@admin_bp.route('/admin/permission/list', methods=['GET'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
@permission_required(ModuleType.ADMIN_ROLE, OperationType.QUERY)
def permission_list():
    page = request.args.get('page', type=int)
    page_size = request.args.get('pageSize', type=int)
    search_key = request.args.get('searchKey', type=str)
    uid = request.args.get('uid', type=int)
<<<<<<< HEAD
    
    # 创建基础查询
    query = Permission.query
    
=======

    # 创建基础查询
    query = Permission.query

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    # 如果有搜索关键词，添加过滤条件
    if search_key:
        try:
            uid_user = User.query.get(uid)
<<<<<<< HEAD
            operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_ROLE,operation=OperationType.QUERY,detail=f'搜索权限：{search_key}')
=======
            operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_ROLE,
                                         operation=OperationType.QUERY, detail=f'搜索权限：{search_key}')
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
            db.session.add(operation_log)
            db.session.commit()
        except Exception as e:
            print(e)
<<<<<<< HEAD
            
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
        query = query.filter(db.or_(
            Permission.module.like(f'%{search_key}%'),
            Permission.operation.like(f'%{search_key}%')
        ))
<<<<<<< HEAD
        
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    # 获取分页对象
    pagination = query.paginate(
        page=page,
        per_page=page_size,
        error_out=False
    )
<<<<<<< HEAD
    
    # 获取当前页的用户列表
    permissions = [permission.to_json() for permission in pagination.items]
    
    # 获取总用户数
    total = pagination.total
    
=======

    # 获取当前页的用户列表
    permissions = [permission.to_json() for permission in pagination.items]

    # 获取总用户数
    total = pagination.total

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    data = {
        'total': total,
        'permissions': permissions
    }
<<<<<<< HEAD
    
    return jsonify({'message':'获取权限列表成功','errcode':'0','data':data}),200


@admin_bp.route('/admin/permission/add',methods=['POST'])
=======

    return jsonify({'message': '获取权限列表成功', 'errcode': '0', 'data': data}), 200


@admin_bp.route('/admin/permission/add', methods=['POST'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
@permission_required(ModuleType.ADMIN_ROLE, OperationType.ADD)
def permission_add():
    module = request.json.get('module')
    operation = request.json.get('operation')
    description = request.json.get('description')
    uid = request.json.get('uid')

<<<<<<< HEAD
    permission = Permission(module=module,operation=operation,description=description)
    uid_user = User.query.get(uid)
    operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_ROLE,operation=OperationType.ADD,detail=f'添加权限：{module} {operation}')
=======
    permission = Permission(
        module=module, operation=operation, description=description)
    uid_user = User.query.get(uid)
    operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_ROLE,
                                 operation=OperationType.ADD, detail=f'添加权限：{module} {operation}')
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    db.session.add(permission)
    db.session.add(operation_log)
    try:
        db.session.commit()
<<<<<<< HEAD
        return jsonify({'message':'添加权限成功','errcode':'0'}),200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message':'添加权限失败','errcode':'-1','error':str(e)}),500
    
    
@admin_bp.route('/admin/permission/edit',methods=['PUT'])
=======
        return jsonify({'message': '添加权限成功', 'errcode': '0'}), 200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message': '添加权限失败', 'errcode': '-1', 'error': str(e)}), 500


@admin_bp.route('/admin/permission/edit', methods=['PUT'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
@permission_required(ModuleType.ADMIN_ROLE, OperationType.UPDATE)
def permission_edit():
    id = request.json.get('id')
    module = request.json.get('module')
    operation = request.json.get('operation')
    description = request.json.get('description')
    uid = request.json.get('uid')

    permission = Permission.query.get(id)
    permission.module = module
    permission.operation = operation
    permission.description = description
    try:
        db.session.commit()
        uid_user = User.query.get(uid)
<<<<<<< HEAD
        operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_ROLE,operation=OperationType.UPDATE,detail=f'更新权限：{module} {operation}')
        db.session.add(operation_log)
        db.session.commit()
        return jsonify({'message':'更新权限成功','errcode':'0'}),200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message':'更新权限失败','errcode':'-1','error':str(e)}),500


@admin_bp.route('/admin/permission/delete',methods=['DELETE'])
=======
        operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_ROLE,
                                     operation=OperationType.UPDATE, detail=f'更新权限：{module} {operation}')
        db.session.add(operation_log)
        db.session.commit()
        return jsonify({'message': '更新权限成功', 'errcode': '0'}), 200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message': '更新权限失败', 'errcode': '-1', 'error': str(e)}), 500


@admin_bp.route('/admin/permission/delete', methods=['DELETE'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
@permission_required(ModuleType.ADMIN_ROLE, OperationType.DELETE)
def permission_delete():
    id = request.json.get('id')
    uid = request.json.get('uid')
    permission = Permission.query.get(id)
    uid_user = User.query.get(uid)
<<<<<<< HEAD
    operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.ADMIN_ROLE,operation=OperationType.DELETE,detail=f'删除权限：{permission.module} {permission.operation}')
=======
    operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.ADMIN_ROLE,
                                 operation=OperationType.DELETE, detail=f'删除权限：{permission.module} {permission.operation}')
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    try:
        db.session.delete(permission)
        db.session.add(operation_log)
        db.session.commit()
<<<<<<< HEAD
        return jsonify({'message':'删除权限成功','errcode':'0'}),200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message':'删除权限失败','errcode':'-1','error':str(e)}),500


=======
        return jsonify({'message': '删除权限成功', 'errcode': '0'}), 200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message': '删除权限失败', 'errcode': '-1', 'error': str(e)}), 500
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
